package org.mikhaylov.cp.service.command.impl;

import javax.servlet.http.HttpServletRequest;

import org.mikhaylov.cp.service.command.ServiceCommand;
import org.mikhaylov.cp.service.db.entities.User;
import org.mikhaylov.cp.service.hibernate.HibernateManager;
import org.mikhaylov.cp.service.response.ResponseType;

public class GetUserInventoriesServiceCommand extends ServiceCommand {

	private static final long serialVersionUID = 5840793295864327592L;

	private User user = null;

	@Override
	protected boolean checkParams(HttpServletRequest request) {
		if (request.getAttribute("user") == null
				|| !(request.getAttribute("user") instanceof User)) {
			response.setMeta("Bad parameters");
			response.setType(ResponseType.ERROR);
			return false;
		}
		return true;
	}

	@Override
	protected boolean extractParams(HttpServletRequest request) {
		user = (User) request.getAttribute("user");
		return true;
	}

	@Override
	protected void process(HttpServletRequest request) {
		request.setAttribute("inventories", HibernateManager.getInstance()
				.getInventories(user));
		response.setMeta("User inventories got");
		response.setType(ResponseType.SUCCESS);
	}

}
